package de.slothsoft.wasbinich.android.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import de.slothsoft.wasbinich.android.AbstractMainActivity;
import de.slothsoft.wasbinich.android.R;

public final class MediaPlayerUtil {

    private static final int[] TITlES = { R.raw.title_first, R.raw.title_loser, R.raw.title_sad };
    private static final int[] MUSICS = { R.raw.music_jump, R.raw.music_monkey_island, R.raw.music_toads_march,
	    R.raw.music_toads_unhappy };

    private static final MediaPlayer MEDIA_PLAYER = new MediaPlayer();

    static {
	MEDIA_PLAYER.setOnPreparedListener(new OnPreparedListener() {

	    @Override
	    public void onPrepared(MediaPlayer mediaPlayer) {
		mediaPlayer.start();

	    }
	});
	MEDIA_PLAYER.setOnCompletionListener(new OnCompletionListener() {

	    @Override
	    public void onCompletion(MediaPlayer mp) {
		restartMusic(ActivityUtil.getCurrentActivity());
	    }
	});
    }

    public static boolean userWantsMusic;
    private static int started;
    private static boolean paused;

    public static void startMusic(Context context) {
	Log.d("slothsoft", "MediaPlayer started "); //$NON-NLS-1$ //$NON-NLS-2$
	if (!userWantsMusic)
	    return;
	started++;
	if (paused) {
	    paused = false;
	    MEDIA_PLAYER.start();
	} else {
	    doStartMusic(context);
	}
    }

    public static void restartMusic(Context context) {
	if (!userWantsMusic)
	    return;

	if (context == null) {
	    Log.d("slothsoft", "MediaPlayer stopped for good."); //$NON-NLS-1$ //$NON-NLS-2$
	    return;
	}
	Log.d("slothsoft", "MediaPlayer restarted"); //$NON-NLS-1$ //$NON-NLS-2$
	doStartMusic(context);
    }

    private static void doStartMusic(Context context) {
	if (MEDIA_PLAYER.isPlaying())
	    return;
	try {
	    MEDIA_PLAYER.reset();
	    AssetFileDescriptor desc = context.getResources().openRawResourceFd(getMusicId(context));
	    MEDIA_PLAYER.setDataSource(desc.getFileDescriptor(), desc.getStartOffset(), desc.getLength());
	    desc.close();

	    MEDIA_PLAYER.prepare();
	} catch (Exception ex) {
	    Log.d("slothsoft", "MediaPlayer starting failed: ", ex); //$NON-NLS-1$ //$NON-NLS-2$
	}
    }

    private static int getMusicId(Context context) {
	if (context instanceof AbstractMainActivity)
	    return getMusicId(TITlES);
	return getMusicId(MUSICS);
    }

    private static int getMusicId(int[] ids) {
	return ids[(int) (Math.random() * ids.length)];
    }

    public static void stopMusic() {
	started--;
	Log.d("slothsoft", "MediaPlayer paused "); //$NON-NLS-1$ //$NON-NLS-2$
	if (started <= 0 && MEDIA_PLAYER.isPlaying()) {
	    MEDIA_PLAYER.pause();
	    paused = true;
	}
    }

    private MediaPlayerUtil() {
	// hide me
    }
}
